//初始化数据
var ties = [];
var is_Login = false;
var uid = "";
var user_now_city = "";



initUsers();
var users = getUsers();


var user;
//初始化登入数据
if(AutoLogin()){
	console.log("自动登入成功!");
	is_Login = true;
	uid = $.cookie('uid');
	$("#login_icon").fadeOut();
	$("#register_icon").fadeOut();
	$("#comment_text").attr('placeholder', '发表的评论内容~');
	user = AutoLogin();
	$('.username').text(user.username);
	has_login = true;
}else{
	$("#quit_icon").fadeOut();
	$('.username').text("未登入");
	$("#comment_body_inner *").attr('disabled', 'true');
	$("#comment_text").attr('placeholder', '请先登入后再抒发评论哦~');
}


getWeatherAndLocation();


//如果未存或大于1小时或不存在贴数据,重新获取和存
initTies();
ties = JSON.parse(localStorage.getItem("ties"));
ties = getLatestData();

//渲染内容
resolveData();

//轮播图启动
addBanner();


function resolveData() {
	var insert_html = "";
	var page_num = getUrlValue("page");
	var mode = getUrlValue("mode");
	if (mode == 'new') {
		ties = getLatestData();
	} else if (mode == 'hot') {
		ties = getHotestData();
	} else {
		ties = getRecommandData();
	}


	//初始化页码active
	$("#page_change li").find("active").removeClass("active");
	if ($("#page_change li").eq(page_num - 1) !== null) {
		$("#page_change li").eq(page_num - 1).addClass('active');
	}


	var add_num = 0;
	$.each(ties, function(k, v) {

		var who_publish = "";
		if (v.uid !== '系统推送') {
			if (users.find(function(user) {
					return user.uid === v.uid;
				})) {
				who_publish = users.find(function(user) {
					return user.uid === v.uid;
				}).username;
			} else {
				who_publish = "未知用户";
			}
		} else {
			who_publish = "系统推送";
		}


		if (k >= 5 * (page_num - 1) && k < (5 * page_num)) {
			add_num++;
			if (v.img == null || v.img == undefined || v.img == "") {
				insert_html += `
							<li class="tabCon_Every">
								<strong id="tie_title"><svg class="CustomIcon">
								<use xlink:href="#icon-shengdanwancan"></use>
								</svg><a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a></strong>
								<p id="summary">${v.describe}</p>
								<img src="imgs/NoPic.png" alt="" id="tie_pic" />
								<p id="public_user"><svg class="CustomIcon">
								<use xlink:href="#icon-shengdanqiu"></use>
								</svg>${who_publish}</p>
								<p id="public_time"><svg class="CustomIcon">
								<use xlink:href="#icon-shengdanwazi"></use>
								</svg>${v.hot}</p>
							</li>
							`
			} else {

				insert_html += `
							<li class="tabCon_Every">
								<strong id="tie_title"><svg class="CustomIcon">
								<use xlink:href="#icon-shengdanwancan"></use>
								</svg><a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a></strong>
								<p id="summary">${v.describe}</p>
								<img src="${v.img}" alt="" id="tie_pic" />
								<p id="public_user"><svg class="CustomIcon">
								<use xlink:href="#icon-shengdanqiu"></use>
								</svg>${who_publish}</p>
								<p id="public_time"><svg class="CustomIcon">
								<use xlink:href="#icon-shengdanwazi"></use>
								</svg>${v.hot}</p>
							</li>
							`
			}
		}
	})
	//不足5条则往前补
	while (add_num < 5) {
		//如果超过尾页自动变成尾页
		if (getUrlValue('page') > Math.floor(ties.length / 5) + 1) {
			setIndexPage(Math.floor(ties.length / 5) + 1);
		}
		//如果超过首页自动变成1页
		if (getUrlValue('page') < 1) {
			setIndexPage(1);
		}
		//帖子从最后往前添
		v = ties[(ties.length - 1) + (add_num - 5)];
		insert_html += `
					<li class="tabCon_Every">
						<strong id="tie_title"><svg class="CustomIcon">
						<use xlink:href="#icon-shengdanwancan"></use>
						</svg><a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a></strong>
						<p id="summary">${v.describe}</p>
						<img src="${v.img}" alt="" id="tie_pic" />
						<p id="public_user"><svg class="CustomIcon">
						<use xlink:href="#icon-shengdanqiu"></use>
						</svg>系统推送</p>
						<p id="public_time"><svg class="CustomIcon">
						<use xlink:href="#icon-shengdanwazi"></use>
						</svg>${v.hot}</p>
					</li>
					`
		add_num++;
	}
	$(".tabCon ul").html(insert_html);
}


function addBanner() {
	let arrs = getHotestData(ties).slice(0, 5);
	let nNow = 0;
	$.each(arrs, function(i, e) {
		if ($(".imgs li").eq(i - 1)) {
			$(".imgs li").eq(i - 1).hide();
		}
		$(".imgs li").eq(i).css('background-image', `url("${e.img}")`);
		$(".imgs li").eq(i).click(function() {
			window.open("tie.html?pid=" + e.id);
		})
	});

	function show(n) {
		$(".imgs li").eq(nNow).fadeOut().removeClass("cur");
		$(".imgs li").eq(n).fadeIn().addClass("cur");
		$(".dot li").eq(nNow).removeClass("cur");
		$(".dot li").eq(n).addClass("cur");
		$("#banner .bannertext").eq(0).text(arrs[n].title);
		nNow = n;
	}
	$(".dot li").mouseover(function() {
		show($(this).index());
	})
	let time1 = setTimeout(autoCg, 2000);

	function autoCg() {
		show((nNow + 1) % arrs.length);
		if (time1) clearTimeout(time1);
		time1 = setTimeout(autoCg, 2000);
	}
	$("#banner").hover(function() {
		if (time1) {
			clearTimeout(time1);
		}
	}, function() {
		time1 = setTimeout(autoCg, 2000);
	})
}


$("#comment_button").click(function() {
	$new_obj = {
		'id': Math.floor(Math.random() * 100000000) + 1,
		"title": $("#comment_input").val(),
		'hot': 1,
		"time": Date.now(),
		"img": "",
		"describe": $("#comment_text").val(),
		'uid': uid,
		'location': user_now_city,
		'log': []
	};
	ties.push($new_obj);
	ties = getLatestData();
	localStorage.setItem("ties", JSON.stringify(ties));
	alert("发表成功!");
	setIndexPage('1');
});

$("#page_change li").click(function() {
	setIndexPage($(this).text());
});
$("#page_next").click(function() {
	setIndexPage(parseInt(getUrlValue('page')) + 1);
})
$("#page_prev").click(function() {
	setIndexPage(parseInt(getUrlValue('page')) - 1);
})
$("#page_end").click(function() {
	setIndexPage(Math.floor(ties.length / 5) + 1);
})
$("#page_first").click(function() {
	setIndexPage("1");
})




$("#ChangeTheme").click(function() {

	if ($(this).css('background-color') == 'rgb(255, 255, 255)') {
		$(this).css({
			'background-color': 'black'
		});
		location.reload(true);
	} else {

		$(this).css({
			'background-color': 'white'
		});
		$("#top_ul").css({
			'background-color': 'black'
		}).find("a").css({
			'color': 'white'
		});
		$("#main_body").css({
			'background-color': 'black'
		});
		$("#search_container").css({
			'background-color': 'black'
		}).find("input").css({
			'background-color': 'black'
		}).parent().find("button").css('background-color', 'black');
		$("#top_line").css({
			'background-color': 'black'
		});
		$(".tabNavs").css({
			'background-color': 'black'
		});
		$("#slide_body").css({
			'background-color': 'black'
		});
		$("#comment_body_inner").css({
			'background-color': 'black'
		});
		$("#comment_text").css({
			'background-color': 'black'
		});
		$("#comment_input").css({
			'background-color': 'black'
		});
		$("#comment_button").css({
			'background-color': 'black'
		});
		$("#page_change").css({
			'background-color': 'black'
		});
		$("button").css({
			'background-color': 'black'
		});
		$(".tabCon_Every").css({
			'background-color': 'black'
		});
		$("footer").css({
			'background-color': 'black'
		});
		$('.slideTopic').css({
			'background-color': 'black'
		});
		$('.bannertext').css({
			'background-color': 'black'
		});
		$('#search_result_container').css({
			'background-color': 'grey'
		});

		$.each($('*'), function() {

			if ($(this).css('border').indexOf('none') < 0) {
				$(this).css({
					'border': '1px solid rgb(60,60,60)'
				});
			}
		})
		$('body').css({
			'cursor': 'url("imgs/cur_2.png"),auto'
		});
		$('a').css('color', 'white');
		$('*').css('color', 'white');

	}
})







//禁用原先的a事件 防止干扰跳转url
$(".tabNavs li a").css('pointer-events', 'none');
$(".tabNavs li").click(function() {
	var mode = $(this).text().trim();
	if (mode == '最新') {
		setIndexMode('new');
	} else if (mode == '最热') {
		setIndexMode('hot');
	} else {
		setIndexMode('recommend');
	}

});

var talks = "";
$.each(getHotestData().slice(0, 6), function(k, v) {
	talks += `<li class="talks_box_con"><a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a></li>`
});
$("#talks_box").html(talks);


var publics = "";
$.each(getLatestData().slice(0, 6), function(k, v) {
	publics += `<li class="talks_box_con"><a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a></li>`
});
$("#public_box").html(publics);



$("#search_container_input input").on('focus', function() {
	$("#search_result_container").fadeIn();
}).on('blur', function() {
	$("#search_result_container").fadeOut();
}).on('input', function() {
	var text = $(this).val();
	if (text.trim() == "") {
		$("#search_result_container_navs").html("");
		return;
	}
	var results = "";
	$.each(ties, function(k, v) {
		random_icon = random_css_icons[Math.floor(Math.random() * random_css_icons.length)];
		if (v.title.indexOf(text) > -1 || v.describe.indexOf(text) > -1 || v.uid.indexOf(text) > -1) {
			results += `
			<li id="search_result_container_con">
				<svg class="CustomIcon">
				<use xlink:href="#icon-${random_icon}"></use>
				</svg>
				<a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a>
			</li>
			`;
		}
	})
	$("#search_result_container_navs").html(results);

})

$("#search_button").click(function() {
	window.open($("#search_result_container_navs li").eq(0).find('a').attr('href'));
})






